﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Threading.Tasks;
using PlatWL.Data;
using PlatWL.Services.Tools;
using System.Data;

namespace PlatWL.Services.Services
{
    public class UserManage
    {
        public void User_List(ref Msg_Result msg, int Client, Msg_Page page)
        {
            string where = " 1=1 ";
            if (page.whereParams != null && page.whereParams.Count() > 0)
            {
                foreach (var v in page.whereParams)
                {
                    if (v.Key == "key")
                    {
                        where += string.Format(" and (ID like '%{2}%' or NickName like '%{0}%' or Telephone like '%{1}%' ) ", v.Value, v.Value, v.Value);
                    }
                    if (v.Key == "usertype")
                    {
                        where += string.Format(" and UserType='{0}'", v.Value);
                    }
                }
            }
            int total = 0;
            DataTable dt = new pt_user_accountb().GetDataPager("pt_user_account", "*", page.pagesize, page.pageindex, "CRTime desc", where, ref total);

            msg.result = dt;
            msg.total = total;
        }

        public void User_Model(ref Msg_Result msg,int Client, int ID)
        {
            var usr = new pt_user_accountb().GetEntity(d => d.ID == ID);
            usr.Password = "";

            msg.result = usr;
        }
        public void User_Add(ref Msg_Result msg, int Client, pt_user_account adv)
        {
            bool result = false;
            if (adv.ID == 0)
            {
                //判断手机号是否存在
                var usrc = new pt_user_accountb().ExsSclarSql("select * from pt_user_account where Telephone='" + adv.Telephone + "'");
                if (usrc != "")
                {
                    msg.error = "手机号已存在";
                    return;
                }

                adv.Point = 0;
                adv.Balance = 0;
                adv.UserType = 1;
                adv.Lev = 0;
                adv.UpID = 0;
                adv.Status = 0;
                adv.Deleted = 0;
                adv.CRTime = DateTime.Now;

                result = new pt_user_accountb().Insert(adv);

            }
            else
            {
                var userOld = new pt_user_accountb().GetEntity(p => p.ID == adv.ID);
                //判断手机号是否修改了
                if (userOld != null)
                {
                    if (!string.IsNullOrEmpty(adv.Password))
                    {
                        userOld.Password = Tools.Common.Md5(adv.Password);
                    }
                    if (!string.IsNullOrEmpty(adv.Telephone))
                    {
                        var usrc = new pt_user_accountb().ExsSclarSql("select * from pt_user_account where Telephone='" + adv.Telephone + "' and ID!=" + adv.ID);
                        if (usrc != "")
                        {
                            msg.error = "手机号已存在";
                            return;
                        }
                        else
                        {
                            userOld.Telephone = adv.Telephone;
                        }
                    }
                    userOld.TrueName = adv.TrueName;
                    userOld.NickName = adv.NickName;
                    userOld.UserType = adv.UserType;

                    result = new pt_user_accountb().Update(userOld);

                    

                }


            }
            if (!result)
            {
                msg.error = "更新失败";
            }
        }

        /// <summary>
        /// 禁用/启用
        /// </summary>
        /// <param name="msg"></param>
        /// <param name="Client"></param>
        /// <param name="ID"></param>
        public void User_Enable(ref Msg_Result msg, int Client, int ID)
        {
            new pt_user_accountb().ExsSql("update pt_user_account set Status=case Status when 0 then 9 else 0 end where ID=" + ID);
            
        }


    }
}
